package com.krahets88.hashtable;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;

public class Question387 {
    @Test
    public void test1(){
        Solution387One solution = new Solution387One();
        System.out.println(solution.firstUniqChar("aabb"));
    }
}

class Solution387One {
    public int firstUniqChar(String s) {
        /*
        解法一：map，(字母，出现次数),for循环遍历，最后找谁次数为0 n+n
        解法二：
         */
        HashMap<Character, Integer> characterIntegerHashMap = new HashMap<>();
        for (int i = 0; i < s.length(); i++) {
            if (characterIntegerHashMap.containsKey(s.charAt(i))) {
                characterIntegerHashMap.put(s.charAt(i), characterIntegerHashMap.get(s.charAt(i)) + 1);
            } else {
                characterIntegerHashMap.put(s.charAt(i), 1);
            }
        }
        for (int i = 0; i < s.length(); i++) {
            if(characterIntegerHashMap.get(s.charAt(i))==1){
                return i;
            }
        }
        return -1;
    }
}

class Solution387Two{
    public int firstUniqChar(String s) {
        /*
         */
        HashMap<Character, Boolean> dict = new HashMap<>();
        for(char c:s.toCharArray()){
            dict.put(c,!dict.containsKey(c));
        }
        for(int i=0;i<s.length();i++){
            if(dict.get(s.charAt(i))){
                return i;
            }
        }
        return -1;
    }
}
